﻿using System;
using System.Windows.Forms;
using Kay.LazarToDo.Win._Forms;

namespace Kay.LazarToDo.Win
{
    /// <summary>
    /// 启动窗体
    /// </summary>
    public partial class Startup : Form
    {
        private ProjectList _projectList;

        private LazarToDoList _lazarToDoList;

        private FormWindowState _lastWindowState = FormWindowState.Normal;

        private bool _quit;

        /// <summary>
        /// 初始化启动窗体
        /// </summary>
        public Startup()
        {
            InitializeComponent();
        }

        private void toolStripMenuItemProjectList_Click(object sender, EventArgs e)
        {
            if (_projectList == null || _projectList.IsDisposed)
            {
                GC.Collect();
                _projectList = new ProjectList
                {
                    MdiParent = this
                };
                _projectList.Show();
                return;
            }
            _projectList.Activate();
        }

        private void toolStripMenuItemToDoList_Click(object sender, EventArgs e)
        {
            if (_lazarToDoList == null || _lazarToDoList.IsDisposed)
            {
                GC.Collect();
                _lazarToDoList = new LazarToDoList
                {
                    MdiParent = this
                };
                _lazarToDoList.Show();
                return;
            }
            _lazarToDoList.Activate();
        }

        private void Startup_Load(object sender, EventArgs e)
        {
            toolStripMenuItemToDoList.PerformClick();
        }

        private void Startup_SizeChanged(object sender, EventArgs e)
        {
            if (WindowState != FormWindowState.Minimized)
            {
                _lastWindowState = WindowState;
            }
            else
            {
                if (Visible)
                {
                    HideWindow();
                }
            }
        }

        private void HideWindow()
        {
            //ShowInTaskbar = false;
            Hide();
        }

        private void ShowWindow()
        {
            Show();
            //ShowInTaskbar = true;
        }

        private void Startup_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = !_quit;
            if (!_quit)
            {
                HideWindow();
            }
        }

        private void toolStripMenuItemQuitForNoti_Click(object sender, EventArgs e)
        {
            _quit = true;
            Close();
        }

        private void notifyIcon_Click(object sender, EventArgs e)
        {
            if (!Visible)
            {
                ShowWindow();
            }
            if (WindowState == FormWindowState.Minimized)
            {
                WindowState = _lastWindowState;
            }
            else
            {
                Activate();
            }
        }
    }
}
